package com.whfp.coky.ui.base

import android.content.Intent
import android.os.Bundle
import androidx.viewbinding.ViewBinding
import com.whfp.coky.ui.adpter.NavigationAdapter
import com.whfp.coky.ui.base.vm.NavigationActVM

/**
 *
 * 底部导航界面基类
 *
 * 需要完成：
 * 1、companion object 中完成start方法，传入起始位置界面
 * 2、写initdata方法，实现额外界面相关功能
 *
 * created by coky on 2022/3/17
 */
abstract class BaseNavigationActivity<VM :NavigationActVM<VB>,VB:ViewBinding>  : BaseActivity<VM, VB>(){

    companion object {
        internal val INTENT_KEY_IN_FRAGMENT_INDEX: String = "fragmentIndex"
        internal val INTENT_KEY_IN_FRAGMENT_CLASS: String = "fragmentClass"
    }

    override fun initView() {
        vModel.titleList = buildTitle()
        vModel.itemList.value = buildItems()
        vModel.fragList.value = buildFrags()
        vModel.initSize()
    }

//    设置当前页面
    fun seCurrFrag(){
        onNewIntent(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        vModel.mPagerAdapter?.let {
            vModel.switchFragment(it.getFragmentIndex(getSerializable(INTENT_KEY_IN_FRAGMENT_CLASS)))
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        vModel.getNavigationBody().let {
            // 保存当前 Fragment 索引位置
            outState.putInt(INTENT_KEY_IN_FRAGMENT_INDEX, it.currentItem)
        }
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        // 恢复当前 Fragment 索引位置
        vModel.switchFragment(savedInstanceState.getInt(INTENT_KEY_IN_FRAGMENT_INDEX))
    }

    override fun onDestroy() {
        super.onDestroy()
        vModel.destroyAct()
    }

    abstract fun buildTitle():MutableList<String>

    abstract fun buildItems():MutableList<NavigationAdapter.MenuItem>

    abstract fun buildFrags():MutableList<BaseFragment<*, *>>
}